Skip to content

Add conformsTo argument to member macro expansion operation #2004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 7, 2023

Conversation

DougGregor
Copy link
Member

Stage in an entrypoint for member macros that allows them to learn about which conformances that they've asked about are "missing", meaning that they are not present on the type (ignoring those that would be generated by an extension macro). This information is equivalent to the information provided to extension macros, although the member macro itself cannot create the conformance.

@DougGregor DougGregor requested a review from ahoppen as a code owner August 4, 2023 23:36
@DougGregor
Copy link
Member Author

@swift-ci please test

@@ -236,6 +236,7 @@ public func expandAttachedMacroWithoutCollapsing<Context: MacroExpansionContext>
let members = try attachedMacro.expansion(
of: attributeNode,
providingMembersOf: declGroup,
missingConformancesTo: conformanceList?.map(\.type) ?? [],
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is this parameter called missingConformancesTo:? AFAICT you are passing in the existing conformances, not some list of missing conformances.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm struggling mightily to name this. Let's discussion naming over at https://forums.swift.org/t/pitch-member-macros-that-know-what-conformances-are-missing/66590

@DougGregor
Copy link
Member Author

@swift-ci please test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor force-pushed the member-macro-conformances branch from 8d1c176 to f1eaeec Compare August 19, 2023 06:07
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor changed the title [MemberMacro] Add missingConformancesTo argument to expansion operation Add conformancesTo argument to member macro expansion operation Aug 19, 2023
@DougGregor
Copy link
Member Author

@swift-ci please clean test

@DougGregor DougGregor force-pushed the member-macro-conformances branch from cf38182 to d3a820a Compare September 7, 2023 05:20
@DougGregor
Copy link
Member Author

@swift-ci please test

… operation

Stage in an entrypoint for member macros that allows them to learn
about which conformances that they've asked about are "missing",
meaning that they are not present on the type (ignoring those that
would be generated by an extension macro). This information is
equivalent to the information provided to extension macros, although
the member macro itself cannot create the conformance.
@DougGregor DougGregor force-pushed the member-macro-conformances branch from d3a820a to 5ff03e2 Compare September 7, 2023 15:21
@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor DougGregor changed the title Add conformancesTo argument to member macro expansion operation Add conformsTo argument to member macro expansion operation Sep 7, 2023
@DougGregor
Copy link
Member Author

@swift-ci please test Windows

Copy link
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add the API change to the release notes and add a test case for it?

Comment on lines +57 to +75
public extension MemberMacro {
/// Default implementation supplies no conformances.
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return try expansion(of: node, providingMembersOf: declaration, conformingTo: [], in: context)
}

/// Default implementation that ignores the unhandled conformances.
static func expansion(
of node: AttributeSyntax,
providingMembersOf declaration: some DeclGroupSyntax,
conformingTo protocols: [TypeSyntax],
in context: some MacroExpansionContext
) throws -> [DeclSyntax] {
return try expansion(of: node, providingMembersOf: declaration, in: context)
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t think adding default implementations that forward in both directions is a good way. If I write the following,

public struct X: MemberMacro {}

it compiles just fine without any error that I’m missing any protocol requirement and the call to the expansion function will just infinitely recurse.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can stage out one of them, but for now I want forward/backward compatibility.

@DougGregor DougGregor merged commit 59f04f4 into swiftlang:main Sep 7, 2023
@DougGregor DougGregor deleted the member-macro-conformances branch September 7, 2023 18:26
@DougGregor
Copy link
Member Author

Could you add the API change to the release notes and add a test case for it?

I can do this in a follow-up

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants